package com.lxch.gdwxp.wxpay.utils;

import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class MyPayUtil {

   public static Log log = LogFactory.getLog(MyPayUtil.class);

   /**
    * 
    * @param requestUrl 请求url
    * @param requestMethod 请求方式
    * @param outputStr 请求参数
    * @return 返回的json对象
    */
   public static JSONObject httpsRequestToJsonObject(String requestUrl,
         String requestMethod, String outputStr) {
      JSONObject jsonObject = null;
      try {
         StringBuffer buffer = httpsRequest(requestUrl, requestMethod,
               outputStr);
         System.out.println(buffer.toString() + "totototototototototo");
         jsonObject = JSONObject.fromObject(buffer.toString());
      } catch (ConnectException ce) {
         log.error("连接超时：" + ce.getMessage());
      } catch (Exception e) {
         log.error("https请求异常：" + e.getMessage());
      }
      return jsonObject;
   }

   /**
    * 
    * @param requestUrl
    * @param requestMethod
    * @param output
    * @return
    * @throws NoSuchAlgorithmException
    * @throws NoSuchProviderException
    * @throws KeyManagementException
    * @throws MalformedURLException
    * @throws IOException
    * @throws ProtocolException
    * @throws UnsupportedEncodingException
    */
   private static StringBuffer httpsRequest(String requestUrl,
         String requestMethod, String output)
               throws NoSuchAlgorithmException, NoSuchProviderException,
               KeyManagementException, MalformedURLException, IOException,
               ProtocolException, UnsupportedEncodingException {
      URL url = new URL(requestUrl);
      HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
      connection.setDoOutput(true);
      connection.setDoInput(true);
      connection.setUseCaches(false);
      connection.setRequestMethod(requestMethod);
      //设置  参数  服务器会接收到参数
      connection.setRequestProperty("Content-Type",
            "application/Json; charset=UTF-8");
      if (null != output) {
         OutputStream outputStream = connection.getOutputStream();
         outputStream.write(output.getBytes("utf-8"));
         outputStream.close();
      }
      // 从输入流读取返回内容
      InputStream inputStream = connection.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      String str = null;
      StringBuffer buffer = new StringBuffer();
      while ((str = bufferedReader.readLine()) != null) {
         buffer.append(str);
      }
      bufferedReader.close();
      inputStreamReader.close();
      inputStream.close();
      inputStream = null;
      connection.disconnect();
      return buffer;
   }
}
